import axios from 'axios';
import {
    AxiosRequestConfig,
    AxiosInstance,
} from 'axios'

import NProgress from 'nprogress';
import 'nprogress/nprogress.css'

export const createAxiosByInterceptors = (
    config?: AxiosRequestConfig
): AxiosInstance => {
    const instance = axios.create({
        // baseURL: "http://localhost:8088/",
        withCredentials: true,
        //  请求超时时间
        timeout: Number(process.env.VUE_APP_TIMEOUT),
        //  自定义请求头
        headers: {
            'Content-Type': 'application/json',
            Accept: "application/json",
        },
        ...config,
    });
    
    instance.interceptors.request.use(function (config) {
        /// TODO 发请求之前做点什么
        NProgress.start();
        return config;
    }, function (error) {
        NProgress.done();
        return Promise.reject(error);
    })
    
    instance.interceptors.response.use(function (config) {
        /// TODO 发请求之前做点什么
        NProgress.done();
        return config;
    }, function (error) {
        NProgress.done();
        return Promise.reject(error);
    })
    return instance;
}